package com.techfansy.moreImageSelected;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;

import com.example.alltoollib.R;
import com.techfansy.bottomDialog.BottomDialog;
import com.techfansy.moreImageSelected.bean.ImageItem;
import com.techfansy.moreImageSelected.ui.ImageGridActivity;
import com.techfansy.moreImageSelected.ui.ImagePreviewActivity;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;

public class MoreImageMethod {
    private final int IMAGE_PICKER = 100;
    public List<ImageItem> images = new ArrayList<>(); //获取到的图片集合（相册选择和拍照）
    public boolean isOrigin = false;
    private boolean isOrigin1 = false;

    //弹框中item的点击事件

    public void itemClick(final Activity activity, int i, final List<ImageItem> mList, final ImagePicker imagePicker) {
        //加号点击事件
        if (i == mList.size()) {
            new BottomDialog(activity).addItemSheet(R.string.photograph, new BottomDialog.SheetListener() {
                @Override
                public void setOnSheet(int i) {
                    ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.CAMERA}, 1);
                    PackageManager pm = activity.getPackageManager();
                    boolean permission = (PackageManager.PERMISSION_GRANTED ==
                            pm.checkPermission("android.permission.RECORD_AUDIO", "packageName"));
                    if (permission) {
                        //进入拍照
                        Toast.makeText(activity, "权限被禁止，无法打开相机", Toast.LENGTH_SHORT).show();
                    } else {
                        imagePicker.takePicture(activity, ImagePicker.REQUEST_CODE_TAKE);
                    }
                }
            }).addItemSheet(R.string.select_in_photo_album, new BottomDialog.SheetListener() {
                @Override
                public void setOnSheet(int i) {
                    Intent intent = new Intent(activity, ImageGridActivity.class);
                    intent.putExtra(ImagePreviewActivity.ISORIGIN, isOrigin1);
                    intent.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (Serializable) mList);
                    activity.startActivityForResult(intent, IMAGE_PICKER);
                }
            }).show();

        } else {
            //item点击事件
            Intent intent = new Intent(activity, ImagePreviewActivity.class);
            intent.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (Serializable) mList);
            intent.putExtra(ImagePreviewActivity.ISORIGIN, isOrigin1);
            intent.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, i);
            activity.startActivityForResult(intent, ImagePicker.RESULT_CODE_ITEMS);
        }
    }


    public void setClick(final Activity activity, final List<ImageItem> mList, final ImagePicker imagePicker) {

        new BottomDialog(activity).addItemSheet(R.string.photograph, new BottomDialog.SheetListener() {
            @Override
            public void setOnSheet(int i) {
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA}, 1);
                PackageManager pm = activity.getPackageManager();
                boolean permission = (PackageManager.PERMISSION_GRANTED ==
                        pm.checkPermission("android.permission.RECORD_AUDIO", "packageName"));
                if (permission) {
                    //进入拍照
                    Toast.makeText(activity, "权限被禁止，无法打开相机", Toast.LENGTH_SHORT).show();
                } else {
                    imagePicker.takePicture(activity, ImagePicker.REQUEST_CODE_TAKE);
                }
            }
        }).addItemSheet(R.string.select_in_photo_album, new BottomDialog.SheetListener() {
            @Override
            public void setOnSheet(int i) {
                Intent intent = new Intent(activity, ImageGridActivity.class);
                intent.putExtra(ImagePreviewActivity.ISORIGIN, isOrigin1);
                intent.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (Serializable) mList);
                activity.startActivityForResult(intent, IMAGE_PICKER);
            }
        }).show();
    }

    public void setGallery(Activity activity, List<ImageItem> mList) {
        Intent intent = new Intent(activity, ImageGridActivity.class);
        intent.putExtra(ImagePreviewActivity.ISORIGIN, isOrigin1);
        intent.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (Serializable) mList);
        activity.startActivityForResult(intent, IMAGE_PICKER);
    }

    public void setPhoto(Activity activity, ImagePicker imagePicker) {
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA}, 1);
        PackageManager pm = activity.getPackageManager();
        boolean permission = (PackageManager.PERMISSION_GRANTED ==
                pm.checkPermission("android.permission.RECORD_AUDIO", "packageName"));
        if (permission) {
            //进入拍照
            Toast.makeText(activity, "权限被禁止，无法打开相机", Toast.LENGTH_SHORT).show();
        } else {
            imagePicker.takePicture(activity, ImagePicker.REQUEST_CODE_TAKE);
        }
    }

    //回调中获取到的数据
    public List<ImageItem> onActivityResult(int requestCode, int resultCode, Intent data, final ImagePicker imagePicker, List<ImageItem> mList, Activity activity) {
//        images.clear();
        //相册选择获取的照片显示
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {//返回多张照3片
            if (data != null) {
                //是否发送原图
                isOrigin1 = data.getBooleanExtra(ImagePreviewActivity.ISORIGIN, false);
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
            }
        }

        //拍照获取的照片显示
        if (resultCode == RESULT_OK && requestCode == ImagePicker.REQUEST_CODE_TAKE) {
            //发送广播通知图片增加了
            ImagePicker.galleryAddPic(activity, imagePicker.getTakeImageFile());
            ImageItem imageItems = new ImageItem();
            imageItems.path = imagePicker.getTakeImageFile().getAbsolutePath();

            File file = new File(imageItems.path);
            if (file.exists()) {
                imageItems.name = file.getName();
                imageItems.addTime = file.lastModified() / 1000;
                imageItems.size = file.length();

            }


            imagePicker.clearSelectedImages();
            imagePicker.addSelectedImageItem(0, imageItems, true);

            images = imagePicker.getSelectedImages();
        } else mList.clear();

        //循环添加到mList中
        for (ImageItem imageItem : images) {
            imageItem.setPaths(imageItem.path);
            mList.add(imageItem);
        }

//        mAdapter.upDataList(mList);
        return mList;
    }
}
